package com.liu.servlet;

import com.liu.Bean.User;
import com.liu.dao.impl.UserDaolmpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/login")
public class Login extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        response.setContentType("text/html;charset = utf-8");

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        UserDaolmpl userDaolmpl = new UserDaolmpl();
        User loging = userDaolmpl.Login(username,password);
        System.out.println(loging);
        HttpSession session = request.getSession();

        if (loging != null){
            session.setAttribute("user",loging);
            request.getRequestDispatcher("home.jsp").forward(request,response);
        }else {
            session.setAttribute("error","用户名或密码错误");
            response.sendRedirect("index.jsp");
        }
    }
}
